package com.idp.web.test.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.idp.common.base.BaseController;
import com.idp.common.persistence.Page;
import com.idp.common.util.ValidateUtils;
import com.idp.web.test.entity.Product;
import com.idp.web.test.entity.ProductStatus;
import com.idp.web.test.entity.ProductType;
import com.idp.web.test.entity.Put;
import com.idp.web.test.entity.Warehouse;
import com.idp.web.test.service.DetailListService;
import com.idp.web.test.service.ProductService;
import com.idp.web.test.service.ProductStatusService;
import com.idp.web.test.service.ProductTypeService;
import com.idp.web.test.service.PutService;
import com.idp.web.test.service.WarehouseService;

import net.sf.json.JSONObject;

/**
 * 
 * 产品controller
 * 
 * <pre>
 * 	历史记录：
 * 	2017-06-02 Amber
 * 	新建文件
 * </pre>
 * 
 * @author
 * 
 * 		<pre>
 * SD
 * 	Amber
 * PG
 *	Amber
 * UT
 *
 * MA
 *         </pre>
 * 
 * @version $Rev$
 *
 *          <p/>
 *          $Id$
 *
 */
@Controller
@RequestMapping("/product")
public class ProductController extends BaseController {

	private Logger logger = Logger.getLogger(ProductController.class);

	@Resource
	private ProductService productService;

	@Resource
	private PutService putService;

	@Resource
	private DetailListService detailListService;

	@Resource
	private WarehouseService warehouseService;

	@Resource
	private ProductStatusService productStatusService;

	@Resource
	private ProductTypeService productTypeService;

	/**
	 * 
	 * <pre>
	 * 	2017-06-02 Amber
	 * 	初始化查询页面
	 * </pre>
	 * 
	 * @return
	 */
	@RequestMapping("/init")
	public String init(HttpServletRequest request) {

		request.setAttribute("warehouses", warehouseService.findBySearch(new Warehouse()));
		request.setAttribute("productStatuses", productStatusService.findBySearch(new ProductStatus()));
		request.setAttribute("productTypes", productTypeService.findBySearch(new ProductType()));

		return "shopping/product/productSearch";
	}

	@RequestMapping("/searchList")
	public String searchList(Integer warehouseId, Integer productStatusId, String productName, Page<Product> page,
			HttpServletRequest request) {
		if (warehouseId == 0) {
			warehouseId = null;
		}
		if (productStatusId == 0) {
			productStatusId = null;
		}
		if ("".equals(productName)) {
			productName = null;
		}
		try {
			request.setAttribute("page", productService.sreachByWarehouseAndProductNameAndProductStatus(warehouseId,
					productStatusId, productName, page));
			// request.setAttribute("page", productService.findByPage(product,
			// page));
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}
		return "shopping/product/productList";
	}

	/**
	 * 
	 * <pre>
	 * 	2017-06-02 Amber
	 * 	新增修改页面初始化
	 * </pre>
	 * 
	 * @param id
	 * @param request
	 * @return
	 */
	@RequestMapping("/product")
	public String product(Integer id, String show, HttpServletRequest request) {
		if (show != null && !"".equals(show)) {
			request.setAttribute("msg", "show");
		}
		try {
			if (ValidateUtils.isNotEmpty(id)) {
				Product product = productService.getById(id);
				request.setAttribute("product", product);
			}
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}
		return "shopping/product/product";
	}

	@RequestMapping("/productShelves")
	public String productShelves(Integer id, Product product, HttpServletRequest request) {
		try {
			if (ValidateUtils.isNotEmpty(id)) {
				product = productService.getById(id);
				System.out.println(product);
				request.setAttribute("Product", product);
			}
		} catch (Exception e) {

			logger.error(e.getMessage(), e);
		}

		return "shopping/product/productShelves";
	}

	/**
	 * 
	 * <pre>
	 * 	2017-06-02 Amber
	 * 	保存
	 * </pre>
	 * 
	 * @param product
	 * @return
	 */
	@RequestMapping("/save")
	@ResponseBody
	public String save(Product product, Put put) {
		JSONObject json = new JSONObject();
		try {
			// 修改
			if (ValidateUtils.isNotEmpty(product.getId())) {
				productService.updateNotNull(product);
			}
			// 新增
			else {
				product.setProductStatusId(4);
				productService.add(product);
			}
			json.put("result", "save_success");
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			json.put("result", "save_fail");
		}
		return json.toString();
	}

	/**
	 * 
	 * <pre>
	 * 	2017-06-02 Amber
	 * 	删除
	 * </pre>
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping("/delete")
	@ResponseBody
	public String delete(Integer id) {

		JSONObject json = new JSONObject();

		try {

			productService.delete(id);
			json.put("result", "delete_success");
		} catch (Exception e) {

			logger.error(e.getMessage(), e);
			json.put("result", "delete_fail");
		}
		return json.toString();
	}

	/**
	 * 根据商品类型查看商品
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping("/listByTypeId")
	@ResponseBody
	public String listByTypeId(Product product) {
		JSONObject jsonObject = new JSONObject();
		List<Product> products = productService.findBySearch(product);
		jsonObject.put("products", products);
		jsonObject.put("result", "success");
		return jsonObject.toString();
	}
	
	/**
	 * 根据商品ID查询商品
	 * @param productId
	 * @return
	 */
	@RequestMapping("/getProductById")
	@ResponseBody
	public String getProductById(Integer productId) {
		JSONObject jsonObject = new JSONObject();
		Product product = productService.getById(productId);
		jsonObject.put("product", product);
		return jsonObject.toString();
	}
}